package com.szcinda.fitness;

import com.szcinda.fitness.dto.CoachDto;
import com.szcinda.fitness.dto.CoachRegistrationDto;
import com.szcinda.fitness.dto.RelationshipDto;
import com.szcinda.fitness.persistence.Coach;
import com.szcinda.fitness.persistence.Member;
import com.szcinda.fitness.persistence.Relationship;
import com.szcinda.fitness.persistence.Trainee;
import com.szcinda.fitness.utils.EncodeDecodeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CoachServiceImpl implements CoachService {

    private final CoachRepository coachRepository;
    private final RelationshipRepository relationshipRepository;
    private final TraineeRepository traineeRepository;
    private final MemberRepository memberRepository;

    public CoachServiceImpl(CoachRepository coachRepository, RelationshipRepository relationshipRepository,
                            TraineeRepository traineeRepository, MemberRepository memberRepository) {
        this.coachRepository = coachRepository;
        this.relationshipRepository = relationshipRepository;
        this.traineeRepository = traineeRepository;
        this.memberRepository = memberRepository;
    }

    @Override
    public void registration(CoachRegistrationDto registrationDto) {
        Assert.notNull(registrationDto.getPhone(), "电话号码必须填写");
        Coach existPhone = coachRepository.findByPhone(registrationDto.getPhone());
        Assert.notNull(existPhone, "数据库没有此电话号码的教练信息,请联系管理人员");
        existPhone.setNickName(EncodeDecodeUtil.encode(registrationDto.getNickName()));
        existPhone.setOpenId(registrationDto.getOpenId());
        existPhone.setAvatarUrl(registrationDto.getAvatarUrl());
        coachRepository.save(existPhone);
    }

    @Override
    public CoachDto getCoach(String openId) {
        Coach coach = coachRepository.findByOpenId(openId);
        CoachDto dto = new CoachDto();
        BeanUtils.copyProperties(coach, dto);
        dto.setNickName(EncodeDecodeUtil.decode(coach.getNickName()));
        int traineeCount = relationshipRepository.countByCoachOpenId(openId);
        dto.setTraineeCount(traineeCount);
        return dto;
    }

    @Override
    public boolean checkIsExist(String openId) {
        return coachRepository.findByOpenId(openId) != null;
    }

    @Override
    public void addTrainee(Relationship relationship) {
        Trainee trainee = traineeRepository.findByOpenId(relationship.getTraineeOpenId());
        Assert.notNull(trainee, "此学员还没登录过小程序");
        Relationship existRelationship = relationshipRepository.findByCoachOpenIdAndTraineeOpenId(relationship.getCoachOpenId(), trainee.getOpenId());
        Assert.isNull(existRelationship,"当前用户已经是你的学员,请勿重复添加");
        relationship.setTraineeName(trainee.getNickName());
        relationship.setTraineeImg(trainee.getAvatarUrl());
        relationship.setCreateDate(LocalDateTime.now());
        relationshipRepository.save(relationship);
        //创建会员
        Member existMember = memberRepository.findByOpenId(relationship.getTraineeOpenId());
        if(existMember!=null){
            existMember.setNickName(relationship.getTraineeName());
            existMember.setAvatarUrl(relationship.getTraineeImg());
            memberRepository.save(existMember);
        }else{
            Member member = new Member();
            member.setOpenId(relationship.getTraineeOpenId());
            member.setNickName(relationship.getTraineeName());
            member.setAvatarUrl(relationship.getTraineeImg());
            memberRepository.save(member);
        }
    }

    @Override
    public List<CoachDto> query() {
        List<Coach> coaches = coachRepository.findAll();
        List<CoachDto> coachDtos = new ArrayList<>();
        coaches.forEach(item -> {
            //教练注册才在学员端小程序显示
            if(!StringUtils.isEmpty(item.getOpenId())){
                CoachDto dto = new CoachDto();
                BeanUtils.copyProperties(item, dto);
                coachDtos.add(dto);
            }
        });
        return coachDtos;
    }

    @Override
    public List<RelationshipDto> traineeList(String openId) {
        List<Relationship> relationships = relationshipRepository.findByCoachOpenId(openId);
        List<RelationshipDto> dtos = new ArrayList<>();
        if(relationships.size()>0){
            relationships.forEach(item->{
                RelationshipDto dto = new RelationshipDto();
                BeanUtils.copyProperties(item,dto);
                dto.setTraineeName(EncodeDecodeUtil.decode(dto.getTraineeName()));
                dtos.add(dto);
            });
        }
        return dtos;
    }
}
